const validate = require('../../utils/validate')
const common = require('../../utils/common')
const { delivery, confirmDelivery } = require('../../services/order')
Page({
  data: {
    orderId: '',
    deliveryCode: '',
    enable: false,
  },
  onLoad: function ({ id }) {
    if (!id) return
    this.setData({
      orderId: id,
    })
  },
  inputChange: function (e) {
    const code = e.detail.value
    this.setData({
      deliveryCode: code,
    }, this.canSubmit)
  },
  canSubmit: function () {
    let enable = false
    if (this.data.deliveryCode.length > 0) {
      enable = true
    } else {
      enable = false
    }
    this.setData({
      enable
    })
    return enable
  },
  onConfirm: function () {
    if (!this.data.enable) return
    // 待提货
    // wx.navigateTo({ url: `/pages/delivery-product/index` })
    // return
    delivery({ orderId: this.data.orderId, deliveryCode: this.data.deliveryCode }).then((res) => {
      if (res.success) {
        common.showToast('提交成功，可交货')
      } else {
        common.showToast(res.message)
      }
      // if (!res.data || JSON.stringify(res.data) === "{}") {
      //   common.showToast(res.message)
      // } else {
      //   wx.navigateTo({ url: `/pages/delivery-product/index?data=${JSON.stringify(res.data)}&id=${this.data.orderId}` })
      // }
    })
  }
})
